<template>
  <div class="reigster-box">
    <!-- <h1>Register</h1> -->
    <div class="reigster">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="userName" label="账号">
          <el-input
            v-model="ruleForm.userName"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- --------------------------------------------- -->
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            placeholder="请输入密码"
            prefix-icon="el-icon-sort-up"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input
            type="password"
            v-model="ruleForm.repassword"
            autocomplete="off"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-sort-up"
          ></el-input>
        </el-form-item>
        <!-- --------------------------------------------- -->
        <el-form-item>
          <el-button
            type="danger"
            class="registerbtn"
            @click="submitForm('ruleForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.repassword !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        userName: '',
        password: '',
        repassword: '',
      },
      rules: {
        userName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' },
        ],
        repassword: [
          { required: true, validator: validatePass2, trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$axios.get('/register', {
            params: {
              userName: this.ruleForm.userName,
              password: this.ruleForm.password,
            },
          })
          // console.log(res)
          if (res.code !== 1) {
            // 注册失败
            return this.$message.error('用户名被占用，请重新输入')
          }
          // 否则，成功
          this.$message.success('注册成功')

          // 清空输入框
          this.$refs[formName].resetFields()

          // 将得到的数据中的token存储到本地
          this.$router.push('/home/login')
        } else {
          console.log('注册失败!!')
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.reigster {
  width: 500px;
  height: 300px;
  margin: 100px auto;
  .el-input {
    margin-bottom: 30px;
  }
  .el-button {
    width: 100%;
  }
}
</style>
